#include<stdio.h>
#include<stdlib.h>

typedef struct ListNode ListNode;
struct ListNode
{
    int val;
    ListNode* next;
};

ListNode* removeNthFromEnd(ListNode* head, int n)
{
    ListNode* nhead = head;
    ListNode* fw = head;
    for(int i=0;i<n;++i)fw = fw->next;
    ListNode* td = head; ListNode* p = NULL;
    while (fw)
    {
        p = td;
        td = td->next;
        fw = fw->next;
    }
    if(!p) nhead = head->next;
    else
    {
        p->next = td->next;
    }
    return nhead;
}